#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/27 0027 22:01
# @Author  : Arliki
# @File    : daomu_d.py

import time
from getpic.items import DaomuItem
from scrapy_redis.spiders import RedisSpider


class DaomudSpider(RedisSpider):
    name = 'daomud'
    redis_key = 'daomu:start_link'

    def __init__(self, *args, **kwargs):
        domain = kwargs.pop('domain', '')
        self.allowed_domains = filter(None, domain.split(','))
        super(DaomudSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        dm = DaomuItem()
        chapter = response.xpath('/html/body/section/div[1]/div/header/h1/text()')[0].extract()
        book = response.xpath('/html/body/section/div[1]/div/header/div/span[2]/a/text()')[0].extract()
        book = book.split(':')
        chapter = chapter.split(' ')
        dm['booktitle'] = chapter[0]
        try:
            dm['chaptername'] = chapter[2]
        except:
            dm['chaptername'] = "无"
        finally:
            dm['chapternum'] = chapter[1]
        dm['bookname'] = book[0]
        dm['chapterurl'] = response.url
        print(dm['chapterurl'], "is done")
        time.sleep(1)
        yield dm
